import {
  Column,
  CreateDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Role } from './role.entity';
import { Goods } from '../../goods/entities/goods.entity';
import { Comment } from '../../goods/entities/comment.entity';
import { Order } from '../../order/entities/order.entity';
import { Address } from '../../address/entities/address.entity';
import { Message } from '../../message/entities/message.entity';
import { Favorite } from '../../favorite/entities/favorite.entity';
import { Community } from '../../community/entities/community.entity';

@Entity({
  name: 'users',
})
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({
    length: 50,
    comment: '用户名',
  })
  username: string;

  @Column({
    length: 50,
    comment: '密码',
  })
  password: string;

  @Column({
    length: 50,
    comment: '邮箱',
  })
  email: string;

  @Column({
    length: 100,
    comment: '头像',
    nullable: true,
  })
  headPic: string;

  @Column({
    length: 50,
    comment: '昵称',
    name: 'nick_name',
  })
  nickName: string;

  @Column({
    length: 20,
    comment: '手机号',
    nullable: true,
  })
  phoneNumber: string;

  @Column({
    comment: '是否冻结',
    default: false,
  })
  isFrozen: boolean;

  @Column({
    comment: '是否是管理员',
    default: false,
  })
  isAdmin: boolean;

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;

  @ManyToMany(() => Role)
  @JoinTable({
    name: 'user_roles',
  })
  roles: Role[];

  // 发布的商品
  @OneToMany(() => Goods, (goods) => goods.seller)
  goods: Goods[];

  // 商品评论
  @OneToMany(() => Comment, (comment) => comment.user)
  comments: Comment[];

  // 购买的订单
  @OneToMany(() => Order, (order) => order.buyer)
  buyOrders: Order[];

  // 销售的订单
  @OneToMany(() => Order, (order) => order.seller)
  sellOrders: Order[];

  // 收货地址
  @OneToMany(() => Address, (address) => address.user)
  addresses: Address[];

  // 消息
  @OneToMany(() => Message, (message) => message.user)
  messages: Message[];

  // 收藏
  @OneToMany(() => Favorite, (favorite) => favorite.user)
  favorites: Favorite[];

  @ManyToOne(() => Community, (community) => community.users)
  community: Community;
}
